//
//  YYSettingView.m
//  Project
//
//  Created by Yown on 2024/2/5.
//

#import "YYSettingView.h"
#import "YYMineCell.h"

#import <BIProjectModule/BIProjectContext.h>
#import <BIToolModule/BIProjectDefine.h>
#import <BIRouteModule/BIRouteManager.h>
#import <BIProjectModule/BITableView.h>

#import <BIProjectModule/BIAppDefine.h>
#import <BIToolModule/UIView+Custom.h>
#import <BIToolModule/NSArray+Helper.h>
#import <BIToolModule/NSObject+ViewController.h>

@interface YYSettingView () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *logoutButton;
@property (weak, nonatomic) IBOutlet BITableView *tableView;
@property (nonatomic, copy) NSArray *cellArray;

@end

@implementation YYSettingView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor loadColorFromString:@"#F6F6F6"];
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"YYMineCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:[YYMineCell cellIdentifier]];
    
    self.logoutButton.backgroundColor = [UIColor clearColor];
    self.logoutButton.layer.masksToBounds = YES;
    self.logoutButton.layer.cornerRadius = 28;
    self.logoutButton.layer.borderWidth = 1;
    self.logoutButton.layer.borderColor = [UIColor loadColorFromString:@"#EB5757"].CGColor;
    [self.logoutButton setTitle:BILocalizedString(@"setting_logout") forState:UIControlStateNormal];
    [self.logoutButton setTitleColor:[UIColor loadColorFromString:@"#EB5757"] forState:UIControlStateNormal];
}

- (void)reloadView:(NSArray *)cellArray {
    self.cellArray = cellArray;
    [self.tableView reloadData];
}

#pragma mark - UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [YYMineCell cellHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YYMineCellModel *model = [self.cellArray safeObjectAtIndex:indexPath.row];
    YYMineCell *cell = [tableView dequeueReusableCellWithIdentifier:[YYMineCell cellIdentifier] forIndexPath:indexPath];
    [cell setupModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        NSString *url = @"http://earjoy.watchfun.cn/app-about-h5?code=twj_privacy_policy";
        [[BIRouteManager sharedInstance] openVCURL:url isPresent:NO arguments:nil];
    } else {
        // 注销和退出登录效果一样
        [[NSObject currentVC].navigationController popToRootViewControllerAnimated:YES];
        [[BIProjectContext sharedInstance] logout];
    }
}

- (IBAction)clickLogoutButton:(id)sender {
    // 注销和退出登录效果一样
    [[NSObject currentVC].navigationController popToRootViewControllerAnimated:YES];
    [[BIProjectContext sharedInstance] logout];
}


@end
